﻿/* 
 *  <copyright file="PhysicsMovementBase.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;

using Microsoft.Xna.Framework;




namespace SobrietyEngine.Components.Physics.Movers
{

    public class PhysicsMovementBase: BaseComponent, IComponent, IMovement
    {
        protected IFarseerPhysicsBody physCmp;
        protected IPosition posCmp;

        /// <summary>
        /// Gets or sets a value indicating whether [targetless movement] is allowed. if it is allowed
        /// movement is done in the diretion of 'dir' as long as 'HasTarget' is false
        /// </summary>
        /// <value>
        ///   <c>true</c> if [targetless movement]; otherwise, <c>false</c>.
        /// </value>
        public Boolean TargetlessMovement { get; set; }
        public Vector2 Dir { get; set; }

        public Boolean HasTarget { get; set; }
        public Vector2 TargetV2 { get; private set; }

        float targetRange = 0.05f; //how close we need to get to our target 
        public float TargetRange { get { return targetRange; } set { targetRange = value; } }               

        protected float maxVel = 1.0f;
        public Boolean ClampVelocity { get; set; }
        protected Vector2 maxVelocityClamp = Vector2.One;
        public float MaximumVelocity { get { return maxVel; } set { maxVel = value; maxVelocityClamp = Vector2.One * value;  } }

        protected float maxAngularVel = 1.0f;
        public float MaximumAngularVelocity { get { return maxAngularVel; } set { maxAngularVel = value; } }

        public PhysicsMovementBase()
            : base()
        {
            HasTarget = false;
            TargetV2 = Vector2.Zero;
            TargetlessMovement = false;
        }

        public PhysicsMovementBase(Boolean targetlessMovement, Vector2 dir)
            : base()
        {
            TargetlessMovement = targetlessMovement;
            Dir = dir;
        }

        public override Type[] SupportedInterfaces()
        {
            return new Type[] { typeof(IMovement) };
        }

        public override bool InitComponent()
        {
            base.InitComponent();

            physCmp = Actor.Get<IFarseerPhysicsBody>();
            if (physCmp == null)
            {
                Logging.Logger.GetSingleton().WriteError(this, "Couldnt find FarseerPhysicsCmp on parent actor");
                return false;
            }

            posCmp = Actor.Get<IPosition>();
            if (posCmp == null)
            {
                Logging.Logger.GetSingleton().WriteError(this, "Couldnt find PositionCmp on parent actor");
                return false;
            }

            //initialize some variables
            //get a vector for our max velocity
            maxVelocityClamp = Vector2.One * maxVel;

            return true;
        }

        public void SetTarget(Vector2 target)
        {
            HasTarget = true;
            TargetV2 = target;
        }

        public void Brake()
        {
            physCmp.Body.LinearVelocity = Vector2.Zero;
        }
    }
}
